package com.johnny.bootlaunch.controller;

import com.johnny.bootlaunch.config.ParamValidator;
import com.johnny.bootlaunch.dto.StudentInfo;
import com.johnny.bootlaunch.exception.ParamValidateException;
import com.johnny.bootlaunch.model.AjaxResponse;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;




@RestController
public class ValidatorController {

    private static final Logger logger = LoggerFactory.getLogger(ValidatorController.class);

    @Autowired
    ParamValidator paramValidator;


    @ApiOperation(value = "validator", notes = "验证", tags = "Article",httpMethod = "POST")
    @ApiResponses({
            @ApiResponse(code=200,message="成功",response= AjaxResponse.class),
            @ApiResponse(code=400,message="用户输入错误",response=AjaxResponse.class),
            @ApiResponse(code=500,message="系统内部错误",response=AjaxResponse.class)
    })
    @PostMapping(value = "/validator/info")
    public AjaxResponse validatorInfo(@RequestBody(required = false) StudentInfo studentInfo ){
        try {
            paramValidator.customValidator(studentInfo);
            logger.info(studentInfo.toString());
            return  AjaxResponse.success(studentInfo);
        } catch (ParamValidateException e) {
            logger.error(String.format("FunctionName:%s,Message:%s","validatorInfo",e.toString()));
            return  AjaxResponse.error(e.getMessage());
        }
    }
}
